package com.sk.weichat.ui.me.wallet;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import com.sk.weichat.R;
import com.sk.weichat.ui.base.EditBaseActivity;
import com.sk.weichat.view.PasswordInputEditText;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
* 设置支付密码
* */
public class SetPayPwdActivity extends EditBaseActivity implements View.OnClickListener {

    private ImageView ivBack;
    private TextView tv_title_center;
    private PasswordInputEditText edtPswd;
    private String PayPasswordType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_pay_pwd);
        getSupportActionBar().hide();

        PayPasswordType = getIntent().getStringExtra("PayPasswordType");
        initView();
    }

    private  void initView(){
        ivBack = findViewById(R.id.iv_title_left);
        tv_title_center = findViewById(R.id.tv_title_center);
        tv_title_center.setText(getString(R.string.set_pay_pwd));
        ivBack.setOnClickListener(this);
        edtPswd = findViewById(R.id.edt_pswd);
        edtPswd.setTextWatcher(new Observer<CharSequence>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(CharSequence charSequence) {
                String pswd = charSequence.toString();
                if (pswd.length() == 6) {
                    startActivity(new Intent(SetPayPwdActivity.this,SetPayPwdInputAgainActivity.class).putExtra("pswd",pswd).putExtra("PayPasswordType",PayPasswordType));
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
        //弹出键盘输入
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }

    @Override
    protected void onPause() {
        super.onPause();
        edtPswd.setText("");
    }

    @Override
    public void onClick(View v) {

        if (v==ivBack){
            finish();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void helloEventBus(String str) {
        if (str.equals("set_pay_psd_success")) {
            finish();
        }
    }
}
